package com.itzhanshen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itzhanshen.mapper.UserMapper;
import com.itzhanshen.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.core.GrantedAuthorityDefaults;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.mapping.Attributes2GrantedAuthoritiesMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 构造查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> condition = queryWrapper.eq(User::getUsername, username);

        User user = userMapper.selectOne(condition);

        return new org.springframework.security.core.userdetails.User(username, user.getPassword()
                , AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles()));
    }
}
